package com.work.yshop.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.work.yshop.R;
import com.work.yshop.adapter.ChatAdapter;
import com.work.yshop.model.ChatMessage;
import com.work.yshop.model.Message;
import com.work.yshop.mvp.contract.ChatContract;
import com.work.yshop.mvp.presenter.ChatPresenter;

import java.util.List;

/**
 * 聊天页面
 */
public class ChatActivity extends AppCompatActivity implements ChatContract.View {
    public static final String EXTRA_MESSAGE = "extra_message";
    public static final String EXTRA_UPDATED_MESSAGE = "updated_message";
    
    private ImageView btnBack;
    private TextView tvTitle;
    private RecyclerView recyclerView;
    private EditText editMessage;
    private View btnSend;
    private ProgressBar progressBar;
    private TextView tvEmpty;
    
    private ChatAdapter adapter;
    private ChatPresenter presenter;
    private Message originalMessage;
    
    public static void start(Context context, Message message) {
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtra(EXTRA_MESSAGE, message);
        context.startActivity(intent);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        
        // 获取传递的消息数据
        originalMessage = getIntent().getParcelableExtra(EXTRA_MESSAGE);
        if (originalMessage == null) {
            Toast.makeText(this, "消息数据错误", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        
        initViews();
        initPresenter();
    }
    
    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        tvTitle = findViewById(R.id.tv_title);
        recyclerView = findViewById(R.id.recycler_view);
        editMessage = findViewById(R.id.edit_message);
        btnSend = findViewById(R.id.btn_send);
        progressBar = findViewById(R.id.progress_bar);
        tvEmpty = findViewById(R.id.tv_empty);
        
        // 设置返回按钮点击事件
        btnBack.setOnClickListener(v -> onBackPressed());
        
        // 设置发送按钮点击事件
        btnSend.setOnClickListener(v -> {
            String content = editMessage.getText().toString().trim();
            if (TextUtils.isEmpty(content)) {
                Toast.makeText(this, "请输入消息内容", Toast.LENGTH_SHORT).show();
                return;
            }
            presenter.sendMessage(content);
        });
        
        // 设置RecyclerView
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setStackFromEnd(true); // 从底部开始显示
        recyclerView.setLayoutManager(layoutManager);
        
        adapter = new ChatAdapter();
        recyclerView.setAdapter(adapter);
    }
    
    private void initPresenter() {
        presenter = new ChatPresenter();
        presenter.attachView(this);
        presenter.initChat(originalMessage);
    }
    
    @Override
    public void initChatInfo(Message message) {
        tvTitle.setText(message.getTitle());
        adapter.setContactImageUrl(message.getImageUrl());
    }
    
    @Override
    public void showChatMessages(List<ChatMessage> chatMessages) {
        if (chatMessages.isEmpty()) {
            tvEmpty.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        } else {
            tvEmpty.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
            adapter.setData(chatMessages);
        }
    }
    
    @Override
    public void addNewMessage(ChatMessage message) {
        tvEmpty.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
        adapter.addMessage(message);
    }
    
    @Override
    public void scrollToLatestMessage() {
        recyclerView.smoothScrollToPosition(adapter.getItemCount() - 1);
    }
    
    @Override
    public void clearInputBox() {
        editMessage.setText("");
    }
    
    @Override
    public void showSendFailed(String errorMsg) {
        Toast.makeText(this, errorMsg, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }
    
    @Override
    public void hideLoading() {
        progressBar.setVisibility(View.GONE);
    }
    
    @Override
    public void showError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
    
    @Override
    public void onBackPressed() {
        // 更新原始消息
        presenter.updateOriginalMessage(originalMessage, presenter.getLatestContent());
        
        // 将更新后的消息传回给调用者
        Intent resultIntent = new Intent();
        resultIntent.putExtra(EXTRA_UPDATED_MESSAGE, originalMessage);
        setResult(RESULT_OK, resultIntent);
        
        super.onBackPressed();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
} 